import logging

from base.connection import get_db_session
from conf import config

logger = logging.getLogger(__name__)


def get_goods(goods_id):
    """
    获取平台商品记录
    """
    class Goods(object):
        pass

    session = get_db_session(
        [
            {"db_table": "sdet_goods", "model": Goods},
        ]
    )

    filter = dict(
        sdef_goods_id=goods_id,
        sdef_goods_status=1,
        sdef_goods_is_delete=0,
        sdef_goods_factory_uid=config.FACTORY_UID,
    )

    goods = (
        session.query(Goods)
        .filter_by(**filter)
        .first()
    )

    return goods


def is_goods_full_pkg(goods_info_arr):
    """
    订单商品是否全部整箱销售
    """
    for g in goods_info_arr:
        mpq = 6
        goods_id = g["id"]
        count = g["count"]
        _goods = get_goods(goods_id)
        unit = _goods.sdef_goods_unit
        if  not (unit == 6 or unit == 1 and count % mpq == 0):
            # 非整箱6支或按支销售数量不被6整除为非整箱销售
            if goods_id not in [625383639768242, 634290782815034]:
                # 非特殊情况: 628659208670090多宝，三支装 625383639768242 VSOP一支装 634290782815034思佳XO一支装
                if not (goods_id in [628659208670090] and count % 3 == 0):
                    return False
    return True
